using DbaseInfo;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace pgImport
{
    public partial class pgImport : Form
    {
        private SelectDatabase selected_database = null;
        private ImportParams import_parameters = null;
        private Stream import_stream = null;

        public pgImport()
        {
            InitializeComponent();
        }

        #region Event Handlers

        // ----------------------------------------------------------------------------------

        /// <summary>
        /// Display database selection dialog
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void selectdbaseButton_Click(object sender, EventArgs e)
        {
            selected_database = new SelectDatabase();
            selected_database.ShowDialog();

            // Only enable the 'import' button if got kosher dbase settings
            if (selected_database.IsValidConnection)
                selectimportButton.Enabled = true;
        }

        /// <summary>
        /// Handles user selection of an import file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void selectimportButton_Click(object sender, EventArgs e)
        {
            try
            {
                // Ensure any existing import files are closed
                if (import_stream != null)
                    import_stream.Close();

                // Choose an import file via the file open dialog
                if (importOpenFileDialog.ShowDialog() == DialogResult.OK)
                {
                    import_stream = importOpenFileDialog.OpenFile();
                    selecttablesButton.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                MessageBox.Show("Cannot open selected file. Ensure file not already opened by another application", "File Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

        }

        /// <summary>
        /// Handles 'form closed' event & ensure correct tidying up !
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pgImport_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (import_stream != null)
                import_stream.Close();
        }

        /// <summary>
        /// Handle selection of table_list to import to 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void selecttablesButton_Click(object sender, EventArgs e)
        {
            // Retrieve info for selected database
            DatabaseInfo dbase_info = new DatabaseInfo(selected_database.ConnectionString);

            Console.WriteLine("Found " + dbase_info.TableCount + " tables");

            foreach (TableInfo table in dbase_info.Tables)
            {   
                Console.WriteLine("Table Name : " + table.Name);

                foreach (FieldInfo field in table.Fields)
                {
                    Console.WriteLine("\tField Name : <" + field.Name + "> Type : <" + field.Type + ">");
                }
            }

            import_parameters = new ImportParams(selected_database, import_stream);
            import_parameters.ShowDialog();

        }

        // ----------------------------------------------------------------------------------

        #endregion

    }
}